/*
* Copyright 2010 Competentum group
*
* Licensed under the GNU General Public License, Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.gnu.org/licenses/gpl.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.competentum.engines
{
	import com.competentum.data.parser.CSVParser;
	import com.competentum.data.parser.FlashVarsParser;
	import com.competentum.data.parser.XMLParser;
	import com.competentum.events.EWTParserCommandEvent;
	import com.competentum.events.HistoryEvent;
	import com.competentum.windows.ScopesManager;
	import com.competentum.lib.engines.AbstractEngine;
	import com.competentum.lib.events.CommandEvent;
	import com.competentum.lib.events.ParserEngineCommandEvent;
	import com.competentum.lib.parser.InputOutputManager;
	import com.competentum.lib.parser.Opener;
	import com.competentum.lib.parser.ToFileSaver;
	import com.competentum.lib.events.InputOutputDataEvent;
	
	import flash.events.Event;
	import flash.net.FileFilter;
	
	/**
	 * Engine wrapper for InputOutputManager
	 * 
	 * @author Valentine Abanosimov
	 * */
	public class CSVEngine extends AbstractEngine
	{
		public static const TYPE:String = "CSVEngine";
		
		private var ioManager:InputOutputManager;
		private var parser:CSVParser;
		
		public function CSVEngine()
		{
			super(TYPE);
			var saver:ToFileSaver = new ToFileSaver('.csv');
			saver.addEventListener(CommandEvent.ACTIVATE_ENGINE, redispatchEvent);
			parser = new CSVParser();
			ioManager = new InputOutputManager(new FlashVarsParser(), parser, null, new Opener(null, true, [new FileFilter('Comma Separated Values files', '*.csv')]), saver);
			ioManager.addEventListener(InputOutputDataEvent.CONFIGURATION_IS_LOADED, onLoaded);
			ioManager.addEventListener(InputOutputDataEvent.SAVED_TO_FILE, onSavedToFile);
		}
		
		override protected function activate(event:CommandEvent = null):void
		{
			super.activate(event);
			
			var parserEngineEvent:EWTParserCommandEvent = event as EWTParserCommandEvent;
			if (parserEngineEvent)
			{
				if (parserEngineEvent.reason == ParserEngineCommandEvent.START_READING)
				{
					ioManager.load(parserEngineEvent.scope, parserEngineEvent.url);
				}
				if (parserEngineEvent.reason == ParserEngineCommandEvent.START_SAVING_AS_CONFIGURATION)
				{
					ioManager.saveAsToFile(parserEngineEvent.scope);
				}
			}
		}
		
		private function redispatchEvent(event:Event):void
		{
			dispatchEvent(event.clone());
		}
		
		private function onSavedToFile(event:Event):void
		{
			redispatchEvent(event);
		}
		
		private function onLoaded(event:Event):void
		{
			ScopesManager.currentScope.isFileChanged = true;
			redispatchEvent(event);
		}
	}
}